<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Menus in Java Gnome</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, Java Gnome, menus, Java, programming, Linux">
<meta name="description" content="Menus in Java Gnome">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Menus in Java Gnome</h1>

<p>
In this part of the Java Gnome programming tutorial, we will work with menus.
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* top_horizontal */
google_ad_slot = "3327173442";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
A <b>menubar</b> is one of the most common parts of the GUI application. 
It is a group of commands located in various menus.
While in console applications you have to remember all those arcane commands, 
here we have most of the commands grouped into logical parts. These 
are accepted standards that further reduce the amount of time spending 
to learn a new application. 
</p>


<h2>Simple menu</h2>

<p>
In our first example, we will create a menubar with one file menu. 
The menu will have only one menu item. By selecting the item the application quits. 
</p>

<div class="codehead">simplemenu.java</div> 
<pre class="code">
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Menu;
import org.gnome.gtk.MenuBar;
import org.gnome.gtk.MenuItem;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * ZetCode Java Gnome tutorial
 *
 * This program creates a simple menu.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */


public class GSimpleMenu extends Window {


    public GSimpleMenu() {
    
        setTitle("Simple menu");
        
        initUI();
    
        setDefaultSize(250, 200);
        setPosition(WindowPosition.CENTER);
        showAll();
    }    
    
    private void initUI() {
    
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
    
        VBox vbox = new VBox(false, 0);
        
        MenuBar menuBar = new MenuBar();
        MenuItem fileItem = new MenuItem("File");
        menuBar.append(fileItem);
        
        Menu quitMenu = new Menu();
        MenuItem quitItem = new MenuItem("Quit");
        
        quitItem.connect(new MenuItem.Activate() {

            public void onActivate(MenuItem menuItem) {
                Gtk.mainQuit();
            }
        });
        
        quitMenu.append(quitItem);
        fileItem.setSubmenu(quitMenu);
        
        vbox.packStart(menuBar, false, false, 3);
        add(vbox);
    }
    
    public static void main(String[] args) {
        
        Gtk.init(args);
        new GSimpleMenu();
        Gtk.main();   
    }
}
</pre>

<p>
This is a small example with minimal menubar functionality.
</p>

<pre class="explanation">
 MenuBar menuBar = new MenuBar();
</pre>

<p>
<b class="keyword">MenuBar</b> widget is created. 
</p>

<pre class="explanation">
 MenuItem fileItem = new MenuItem("File");
 menuBar.append(fileItem);
</pre>

<p>
Toplevel <b class="keyword">MenuItem</b> is created. 
</p>

<pre class="explanation">
 Menu quitMenu = new Menu();
 MenuItem quitItem = new MenuItem("Quit");
 ...
 quitMenu.append(quitItem);
</pre>

<p>
Quit <b class="keyword">MenuItem</b> is created and appended to the
quit <b class="keyword">Menu</b>.
</p>

<pre class="explanation">
 fileItem.setSubmenu(quitMenu);
</pre>

<p>
Quit menu is set a a submenu of the toplevel file menu item. 
</p>

<pre class="explanation">
 VBox vbox = new VBox(false, 0);
 ...
 vbox.packStart(menuBar, false, false, 3);
 add(vbox);
</pre>

<p>
Unlike in other toolkits, we have to take care of the layout management
of the menubar ourselves. We put the menubar into the vertical box. 
</p>


<br>
<img src="/img/gui/javagnome/simplemenu.png" alt="Simple menu">
<div class="figure">Figure: Simple menu</div>
<br>


<h2>Image menu</h2>

<p>
In the next example, we will further explore menus.
We will use images and a separator.
</p>

<div class="codehead">imagemenu.java</div> 
<pre class="code">
package com.zetcode;

import org.gnome.gtk.Gtk;
import org.gnome.gtk.ImageMenuItem;
import org.gnome.gtk.Menu;
import org.gnome.gtk.MenuBar;
import org.gnome.gtk.MenuItem;
import org.gnome.gtk.SeparatorMenuItem;
import org.gnome.gtk.Stock;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * ZetCode Java Gnome tutorial
 *
 * This program shows images in
 * the menu.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */

public class GImageMenu extends Window {
    public GImageMenu() {
    
        setTitle("ImageMenu");

        initUI();
    
        setDefaultSize(250, 200);
        setPosition(WindowPosition.CENTER);
        showAll();
    }
    
    
    private void initUI() {
    
        VBox vbox = new VBox(false, 0);
        add(vbox);
        
        MenuBar menuBar = new MenuBar();
        MenuItem file = new MenuItem("_File");
       
        Menu fileMenu = new Menu();
        
        ImageMenuItem imnew = new ImageMenuItem(Stock.NEW);
        ImageMenuItem imopen = new ImageMenuItem(Stock.OPEN);
        ImageMenuItem imquit = new ImageMenuItem(Stock.QUIT);
        
        imquit.connect(new MenuItem.Activate() {

            public void onActivate(MenuItem menuItem) {
                Gtk.mainQuit();
            }
        });
        
        fileMenu.append(imnew);
        fileMenu.append(imopen);
        fileMenu.append(new SeparatorMenuItem());
        fileMenu.append(imquit);
        
        file.setSubmenu(fileMenu);
               
        menuBar.append(file);
        vbox.packStart(menuBar, false, false, 3);
    }
    

    public static void main(String[] args) {
        
        Gtk.init(args);
        new GImageMenu();
        Gtk.main();   
    }   
}
</pre>

<p>
The code example shows how to use images and separators in 
menus in Java Gnome.
</p>


<pre class="explanation">
 ImageMenuItem imnew = new ImageMenuItem(Stock.NEW);
 ImageMenuItem imopen = new ImageMenuItem(Stock.OPEN);
 ImageMenuItem imquit = new ImageMenuItem(Stock.QUIT);
</pre>

<p>
<b class="keyword">ImageMenuItem</b> objects are created. The images come from the 
internal stock of images. 
</p>


<pre class="explanation">
fileMenu.append(new SeparatorMenuItem());
</pre>

<p>
This code line creates a separator. It is used to separate menu items
into logical groups. 
</p>


<br>
<img src="/img/gui/javagnome/imagemenu.png" alt="Image menu">
<div class="figure">Figure: Image menu</div>
<br>


<h2>CheckMenuItem</h2>

<p>
A <b class="keyword">CheckMenuItem</b> is a menu item with a check box. 
It can be used to work with boolean properties. 
</p>

<div class="codehead">checkmenuitem.java</div>
<pre class="code">
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.CheckMenuItem;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Label;
import org.gnome.gtk.Menu;
import org.gnome.gtk.MenuBar;
import org.gnome.gtk.MenuItem;
import org.gnome.gtk.Statusbar;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * ZetCode Java Gnome tutorial
 *
 * This program creates checked menu
 * item.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */


public class GCheckMenuItem extends Window {

    private Statusbar statusbar;


    public GCheckMenuItem() {
    
        setTitle("Check menu item");
        
        initUI();
        
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
    
        setDefaultSize(250, 200);
        setPosition(WindowPosition.CENTER);
        showAll();
    }    
    
    private void initUI() {
    
        VBox vbox = new VBox(false, 0);
        
        MenuBar menuBar = new MenuBar();
        MenuItem fileItem = new MenuItem("File");
        menuBar.append(fileItem);
        
        Menu fileMenu = new Menu();
        MenuItem quitItem = new MenuItem("Quit");
        
        quitItem.connect(new MenuItem.Activate() {

            public void onActivate(MenuItem menuItem) {
                Gtk.mainQuit();
            }
        });
        
        fileMenu.append(quitItem);
        fileItem.setSubmenu(fileMenu);
        
        Menu viewmenu = new Menu();
        MenuItem view = new MenuItem("View");
        view.setSubmenu(viewmenu); 

        CheckMenuItem stat = new CheckMenuItem("View Statusbar");
        stat.setActive(true);
        viewmenu.append(stat);

        menuBar.append(view);

        statusbar = new Statusbar();
        statusbar.setMessage("Ready");

        vbox.packStart(menuBar, false, false, 0);
        vbox.packStart(new Label(""), true, false, 0);
        vbox.packStart(statusbar, false, false, 0);

        stat.connect(new MenuItem.Activate() {

            public void onActivate(MenuItem menuItem) {
                CheckMenuItem item = (CheckMenuItem) menuItem;

                if (item.getActive()) {
                    statusbar.show();
                } else {
                    statusbar.hide();
                }
            }
        });

        add(vbox);
    }
    
    public static void main(String[] args) {
        
        Gtk.init(args);
        new GCheckMenuItem();
        Gtk.main();   
    }
}
</pre>

<p>
In our code example we show a check menu item. If the check box is activated,
the statusbar widget is shown. If not, the statusbar is hidden.  
</p>

<pre class="explanation">
CheckMenuItem stat = new CheckMenuItem("View Statusbar");
</pre>

<p>
<b class="keyword">CheckMenuItem</b> widget is created. 
</p>

<pre class="explanation">
 stat.setActive(true);
</pre>

<p>
The <b class="keyword">setActive()</b> method checks/unchecks the 
check menu item. 
</p>

<pre class="explanation">
  if (item.getActive()) {
      statusbar.show();
  } else {
      statusbar.hide();
  }
</pre>

<p>
Depending on the state of the <b class="keyword">CheckMenuItem</b>, 
we show or hide the statusbar widget. 
</p>


<br>
<img src="/img/gui/javagnome/checkmenuitem.png" alt="CheckMenuItem">
<div class="figure">Figure: CheckMenuItem</div>
<br>


<h2>Submenu</h2>

<p>
Our final example demonstrates how to create a submenu in Java Gnome.
</p>

<div class="codehead">submenu.java</div>
<pre class="code">
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Menu;
import org.gnome.gtk.MenuBar;
import org.gnome.gtk.MenuItem;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * ZetCode Java Gnome tutorial
 *
 * This program creates a submenu.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */


public class GSubmenu extends Window {


    public GSubmenu() {
    
        setTitle("Simple menu");
        
        initUI();
    
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
    
        setDefaultSize(250, 200);
        setPosition(WindowPosition.CENTER);
        showAll();
    }    
    
    private void initUI() {
    
        VBox vbox = new VBox(false, 0);
        
        MenuBar menuBar = new MenuBar();
        MenuItem fileItem = new MenuItem("File");
        menuBar.append(fileItem);
        
        Menu fileMenu = new Menu();
        MenuItem quitItem = new MenuItem("Quit");
        
        quitItem.connect(new MenuItem.Activate() {

            public void onActivate(MenuItem menuItem) {
                Gtk.mainQuit();
            }
        });
        
        
        // submenu creation
        Menu imenu = new Menu();

        MenuItem importm = new MenuItem("Import");
        importm.setSubmenu(imenu);
        
        MenuItem inews = new MenuItem("Import news feed...");
        MenuItem ibookmarks = new MenuItem("Import bookmarks...");
        MenuItem imail = new MenuItem("Import mail...");

        imenu.append(inews);
        imenu.append(ibookmarks);
        imenu.append(imail);

        fileMenu.append(importm);
        fileMenu.append(quitItem);
        fileItem.setSubmenu(fileMenu);

        vbox.packStart(menuBar, false, false, 3);
        add(vbox);
    }
    
    public static void main(String[] args) {
        
        Gtk.init(args);
        new GSubmenu();
        Gtk.main();   
    }
}
</pre>

<p>
Submenu creation. 
</p>

<pre class="explanation">
 Menu imenu = new Menu();
</pre>

<p>
A submenu is a <b class="keyword">Menu</b>. 
</p>

<pre class="explanation">
 MenuItem importm = new MenuItem("Import");
 importm.setSubmenu(imenu);
</pre>

<p>
It is a submenu of a menu item, which belogs to toplevel
file menu. 
</p>

<pre class="explanation">
 MenuItem inews = new MenuItem("Import news feed...");
 MenuItem ibookmarks = new MenuItem("Import bookmarks...");
 MenuItem imail = new MenuItem("Import mail...");

 imenu.append(inews);
 imenu.append(ibookmarks);
 imenu.append(imail);
</pre>

<p>
Submenu has it's own menu items. 
</p>


<br>
<img src="/img/gui/javagnome/submenu.png" alt="Submenu">
<div class="figure">Figure: Submenu</div>
<br>

<hr class="btm">

<p>
In this chapter of the Java Gnome programming library, we showed, how to work with menus. 
</p>


<br>
<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified March 12, 2009  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

